package top.meeting.room.common.config;

import com.jfinal.config.Plugins;
import com.jfinal.kit.PathKit;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.druid.DruidPlugin;
import com.jfinal.plugin.ehcache.EhCachePlugin;
import net.dreamlu.event.EventPlugin;
import top.jfinal.jwtTokenPlugin.plugin.JwtTokenPlugin;
import top.meeting.room.module.book.model.Book;
import top.meeting.room.module.book.model.Room;
import top.meeting.room.module.system.model.Force;
import top.meeting.room.module.system.model.Role;
import top.meeting.room.module.system.model.User;
import top.meeting.room.module.system.services.impl.UserService;

/**
 * 我有故事，你有酒么？
 * JKhaled created by yunqisong@foxmail.com 2017/7/20
 * FOR : 配置插件的类
 */
public class ConfigPlugins {

    /**
     * 动态插件插拔配置
     *
     * @param me
     */
    public static void config(Plugins me) {
        me.add(new EventPlugin(true, "top.meeting", true)); /**配置事件驱动插件 top 下的所有包*/
        me.add(new EhCachePlugin());                                                        /**配置缓存插件*/
        me.add(new JwtTokenPlugin(UserService.me));                                         /**配置权限拦截插件*/
        configRecord(me);
    }

    /**
     * 数据库相关插件
     *
     * @param me
     */
    private static void configRecord(Plugins me) {
        if (MainConfig.db == null)
            return;
        String jdbcUrl = "jdbc:mysql://" + MainConfig.db.get("db.host") + ":" + MainConfig.db.get("db.port") + "/"
                + MainConfig.db.get("db.repertory") + "?characterEncoding=utf8&zeroDateTimeBehavior=convertToNull";
        // 配置Druid数据库连接池插件
        DruidPlugin druidPlugin = new DruidPlugin(jdbcUrl,
                MainConfig.db.get("db.username").trim(), MainConfig.db.get("db.password").trim());
        me.add(druidPlugin);
        // 配置ActiveRecord插件
        ActiveRecordPlugin arp = new ActiveRecordPlugin(druidPlugin);
        arp.setShowSql(MainConfig.db.getBoolean("dev"));
        arp.addMapping("t_user", "id", User.class);
        arp.addMapping("t_role", "id", Role.class);
        arp.addMapping("t_force", "id", Force.class);
        arp.addMapping("t_book", "id", Book.class);
        arp.addMapping("t_room", "id", Room.class);
        arp.setBaseSqlTemplatePath(PathKit.getRootClassPath() + "/sql");
        arp.addSqlTemplate("all.sql");
        me.add(arp);
    }


}
